package com.project.util;

import com.project.bean.UserBean;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从session中得到登陆用户
       UserBean loginUser =  ( UserBean)request.getSession().getAttribute("loginUser");
       if (loginUser!=null){//验证通过，允许继续访问目标资源
           return true;
       }
//       //请求转发
//       request.getRequestDispatcher("/login.html").forward(request,response);
//        重定向
       response.sendRedirect("/login");
        return false;
    }
}
